package com.he.hsdyc.config.security.token;

import com.he.hsdyc.entity.Member;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * jwt扩展类
 * Created with IntelliJ IDEA.
 * Member: jc
 * Date: 2019-06-11
 * Time: 15:56
 */
public class JwtTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {

        Map<String,Object> info = new HashMap<>();
        info.put("id", ((Member) oAuth2Authentication.getPrincipal()).getId());
        info.put("username",((Member) oAuth2Authentication.getPrincipal()).getMemberName());
        info.put("tel",((Member) oAuth2Authentication.getPrincipal()).getMobile());
        info.put("gender",((Member) oAuth2Authentication.getPrincipal()).getSex());
        info.put("auth","1,2");

        //设置附加信息
        ((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(info);
        return oAuth2AccessToken;



//        java.lang.ClassCastException: org.springframework.security.core.userdetails.User cannot be cast to model.Member
//        解决：MyUserDetailService去掉过多的处理，比如Role的处理

//        Encoded password does not look like BCrypt
//        解决：https://www.jianshu.com/p/3d87a52048f6

        //集成jwt
//        1>pom引入
//        2>token包下两个类
//        3>AuthorizationServerConfig中配置configure(AuthorizationServerEndpointsConfigurer endpoints)
//        4>configure(AuthorizationServerSecurityConfigurer security)去掉客户端明文密码校验

//        获取token成功，但是访问的时候报错：invalid token，即token无效，
//        解决：去掉AuthorizationServerConfig中注入的自定义TokenStore(RedisTokenStore),改用默认的TokenStore
    }
}
